package pers.cz.chat.utils;

import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import static pers.cz.chat.ui.Notification.showNotification;

/**
 * @program: Jef-Chat
 * @description: 托盘闪烁
 * @author: Cheng Zhi
 * @create: 2025-01-05 20:28
 **/
public class FlashUtils {

    private static TrayIcon trayIcon;

    private static boolean isFlashing = false;

    private static final String NORMAL_ICON_PATH = "feishu.png";

    private static final String EMPTY_ICON_PATH = "transparent.png";

    private JPanel frame;

    public FlashUtils(JPanel frame) {
        this.frame = frame;
    }
    public void createTrayIcon() {
        //Image normalImage = Toolkit.getDefaultToolkit().getImage(FlashUtils.class.getClassLoader().getResource(NORMAL_ICON_PATH).getPath()); // 正常图标
        Image normalImage = Toolkit.getDefaultToolkit().getImage("D:\\vue\\project\\jef-chat\\chat-idea-plugin\\src\\main\\resources\\icons\\feishu.png"); // 正常图标
        trayIcon = new TrayIcon(normalImage, "Jef-Chat");
        trayIcon.setImageAutoSize(true);

        trayIcon.addMouseListener(new MouseAdapter() {
            public void mouseClicked(MouseEvent e) {
                if (e.getButton() == MouseEvent.BUTTON1) {
                    //JOptionPane.showMessageDialog(null, "应用程序正在运行...");
                    frame.setVisible(true);
                    frame.requestFocus();
                }
            }
        });

        try {
            SystemTray.getSystemTray().add(trayIcon);
        } catch (AWTException e) {
            LogUtils.print("TrayIcon could not be added.");
        }
    }

    public void triggerTrayFlashing() {
        if (trayIcon != null && !isFlashing) {
            isFlashing = true;
            SwingUtilities.invokeLater(() -> showNotification("收到一条新消息！"));
            isFlashing = false;
        }
    }

}
